package com.yixiu.study.juc.thread;

/**
 * @CalssName T03_Sleep_Yield_Join
 * @Description
 * @Author yixiu
 * @Date 2021/6/23 19:29
 * @Verison num1.0
 **/
public class T03_Sleep_Yield_Join {

    public static void main(String[] args) {
//        testSleep();
//        testYield();
        testJoin();
    }

    static void testSleep(){
        new Thread(() -> {
            for(int i=0; i<100; i++){
                System.out.println("A" + i);
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }).start();
    }


    static void testYield(){
        new Thread(() -> {
            for(int i=0; i<100; i++){
                System.out.println("A" + i);
                if(i%10 == 0){
                    Thread.yield();
                }
            }
        }).start();

        new Thread(() -> {
            for(int i=0; i<100; i++){
                System.out.println("B" + i);
                if(i%10 == 0){
                    Thread.yield();
                }
            }
        }).start();
    }

    static void testJoin(){
        Thread t1 = new Thread(() -> {
            for(int i=0; i<10; i++){
                System.out.println("A" + i);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                t1.join();
            } catch (InterruptedException e){
                e.printStackTrace();
            }

            for(int i=0; i<10; i++){
                System.out.println("B" + i);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        });


        t2.start();
        t1.start();
    }


}
